<?php
namespace App\Exports;
use App\Models\ActivityJoin;
use App\Models\UsersWechat;
use App\Models\Activity;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;    //设置标题
use Maatwebsite\Excel\Concerns\ShouldAutoSize;      //自动单元格尺寸
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;      //设置单元格数据格式
use Maatwebsite\Excel\Concerns\WithColumnFormatting;       //设置列格式
 

/**
 * 导出
 */
class ActivityExport implements FromCollection,WithHeadings
{

	public function __construct($data= [])
    {
        $this->data = $data;
    }

    public function collection()
    {
    	$data = $this->data;
        $row = Activity::find($data['id']);
        if (!$row) {
            return $this->error('记录不存在');
        }
        $rows = ActivityJoin::with('user')->where('activity_id',$data['id'])->get();
        $cellData = [];
        foreach ($rows as $key => $row) {
            $akey = [
                $key+1,
                $row->title,
                $row->mobile,
                $row->username,
                $row->created_at,
                $row->price,
                $row->is_vip ?'是':'否',
                $row->is_vip,
            ];
            array_push($cellData,$akey);
            unset($akey);
        }
        return collect($cellData);
    }

    //首行标题
    public function headings(): array
    {
        return [
            '序号',
            '姓名',
            '联系方式',
            '报名活动',
            '报名时间',
            '报名费',
            '是否会员',
            '会员编号',
        ];
    }

}